window.medbiq.mvp.activitynodeVisualization = (function () {
 var name = 'activitynodeVisualization';
 var version = '0.5-alpha';

 var initCSS = function () {
  var headID = document.getElementsByTagName ("head")[0];         
  var cssNode = document.createElement ('style');
  cssNode.type = 'text/css'; 
  var css = [
   'div.WireIt-Terminal {',
    'background-image: none;',
    'width: 0px;',
    'height: 0px;',
    '}',
   'div.WireIt-Container {',
    'width: 200px;',
    '}',
   'div.WireIt-Container-ddhandle {',
    'font-size: 80%; !important',
    'overflow: auto; !important',
    '}'
    ].join ('').replace (/\{name\}/g, name);
  if (cssNode.styleSheet) {// IE
     cssNode.styleSheet.cssText = css;
     } else {
            var newStyle = document.createTextNode (css); 
            cssNode.appendChild (newStyle);
            }
  headID.appendChild (cssNode);
  initCSS = function () {};
  };

 var log = function (msg) {

  initCSS ();

  var win = new Ext.Window ({
   width: 400,
   height: 400,
   title: 'Activity Nodes'
   });
  win.show ();

  var layer = new WireIt.Layer ({
    parentEl: win.body.dom
    })
  //layer.startDynamicLayout ();
  

  var containers = [];
  var containerIndex = -1;
  var addContainer = function (title) {
   containers.push ({
    container: layer.addContainer ({
     terminals: [ { offsetPosition: {bottom: 34, left: 25}, editable: false }],
     title: title,
     position: [ Math.floor(Math.random()*500)+10, Math.floor(Math.random()*200)+10 ],
     close: false
     }),
    containerIndex: ++containerIndex,
    elementID: title,
    hitCounter: 1
    });
   containers[containers.length-1].container.setBody ('Hits: 1');
   };

  var wires = [];
  var addWire = function (node1ID, node2ID) {
   wires.push ({
    node1ID: node1ID,
    node2ID: node2ID,
    wire: layer.addWire ({
     src: { moduleId: node1ID, terminalId: 0 },
     tgt: { moduleId: node2ID, terminalId: 0 },
     drawingMethod: "straight"
     })
    });
   };

  var lastContainerIndex = -1;
 
  log = function (msg) {
   var elementID = msg.elementID;
   var indexOf = -1;
   for (var i = 0; i < containers.length; i++)
       if (elementID === containers[i].elementID) {
	  indexOf = containers[i].containerIndex;
          containers[i].container.setBody ('Hits: '+(++containers[i].hitCounter));
          break;
          }
   if (indexOf == -1) {
      addContainer (elementID);
      indexOf = containers[containers.length-1].containerIndex;
      }
   
   if (lastContainerIndex != -1) {
      for (var i = 0; i < wires.length; i++)
          if ((wires[i].node1ID == lastContainerIndex && wires[i].node2ID == indexOf)
              || (wires[i].node2ID == lastContainerIndex && wires[i].node1ID == indexOf)) {
             wires[i].wire.options.width = ++wires[i].wire.options.width;
             wires[i].wire.redraw ();
	     }      
      addWire (lastContainerIndex, indexOf);
      }
   lastContainerIndex = indexOf;
   };
  log (msg);
  };


 // Add as implementation
 medbiq.mvp.log.registerImplementation ({
  eventReceived: function (e) {
   log (e);
   },
  isNamespaceSupported: function (ns) { return ns === medbiq.mvp.log.NS_ACTIVITY_MODEL; }
  });

 return {};
 }) ();